package com.zyb.system.login.contrller;

import com.sun.deploy.uitoolkit.impl.fx.ui.ErrorPane;
import com.zyb.constans.BaseConstants;
import com.zyb.system.employee.domain.Employee;
import com.zyb.system.employee.service.IEmployeeService;
import com.zyb.system.login.domain.PhoneRegister;
import com.zyb.system.user.domain.User;
import com.zyb.system.user.service.IUserService;
import com.zyb.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/register")
@Api(value = "注册的API",description="注册的功能")
public class RegisterController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IUserService userService;
    @Autowired
    private IEmployeeService employeeService;
    @PostMapping("/phone")
    @ApiOperation(value = "注册的接口")
    public AjaxResult register(@RequestBody PhoneRegister register){
        String password = register.getPassword();
        String passwordRepeat = register.getPasswordRepeat();
        //校验手机号
        if(register.getPhone() == null || "".equals(register.getPhone().trim())){
            return AjaxResult.error().setMsg("手机号不能为空,请输入手机号");
        }
        //验证验证码
        if(register.getPhoneCode() == null ||"".equals(register.getPhoneCode().trim())){
            return AjaxResult.error().setMsg("验证码不能为空,请输入验证码");
        }
        //校验密码不能为空
        if(password ==null || "".equals(password.trim())){
            return AjaxResult.error().setMsg("密码不能为空,请输入密码");
        }
        if(passwordRepeat ==null || "".equals(passwordRepeat.trim())){
            return AjaxResult.error().setMsg("确认密码不能为空,请输入重新密码");
        }
        if(!password.equals(passwordRepeat)){
            return AjaxResult.error().setMsg("输入的两次密码不一致,请输入重新密码");
        }
        //验证码验证
        String key = register.getPhone() + BaseConstants.VCode.REGISTER;

        //取出验证码
        Object smsCode = redisTemplate.opsForValue().get(key);
        if(smsCode == null){
            return AjaxResult.error().setMsg("您的手机号验证码已过期，请重新获取");
        }
        if(!register.getPhoneCode().equals(smsCode.toString())){
            return AjaxResult.error().setMsg("手机验证码错误,请重新输入");
        }
        User userPhone = userService.queryByPhone(register.getPhone());
        Employee employeePhone = employeeService.queryByPhone(register.getPhone());
        if(userPhone != null){
            return AjaxResult.error().setMsg("该手机号已被注册或者忘记密码");
        }
        if(employeePhone != null){
            return AjaxResult.error().setMsg("该手机号已被注册或者忘记密码");
        }
        //添加用户
        userService.add(register);
        employeeService.add(register);
        return AjaxResult.success().setMsg("注册成功");
    }
}
